package me.minecraft.plugin.cobblemonclear.util;

import com.cobblemon.mod.common.pokemon.Pokemon;
import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonFilter {

    private final List<String> labelList;
    private final List<Species> speciesList;
    private final boolean shiny;

    public PokemonFilter(@NotNull List<String> labelList, @NotNull List<Species> speciesList, boolean shiny) {
        this.labelList = labelList;
        this.speciesList = speciesList;
        this.shiny = shiny;
    }

    public boolean matches(Pokemon pokemon) {
        if (shiny && pokemon.getShiny()) return true;
        for (String label : labelList) {
            if (pokemon.hasLabels(label)) {
                return true;
            }
        }
        Species species = pokemon.getSpecies();
        return speciesList.contains(species);
    }

    public boolean isEmpty() {
        return labelList.isEmpty() && speciesList.isEmpty();
    }

    @NotNull
    public static PokemonFilter create(@NotNull List<String> list) {
        List<String> labelList = new ArrayList<>();
        List<Species> speciesList = new ArrayList<>();
        boolean shiny = false;
        for (String string : list) {
            if (string.matches("^.+:.+$")) {
                String[] split = string.split(":", 2);
                if (split.length != 2) continue;
                String prefix = split[0];
                if ("label".equalsIgnoreCase(prefix)) {
                    labelList.add(split[1]);
                } else if ("species".equalsIgnoreCase(prefix)) {
                    Species species = PokemonDataCache.getInstance().getSpeciesByKey(split[1]);
                    if (species == null) continue;
                    speciesList.add(species);
                }
                continue;
            }
            if (string.equalsIgnoreCase("shiny")) shiny = true;
        }
        return new PokemonFilter(labelList, speciesList, shiny);
    }

}
